package org.senseframework.support.open.api.controller.encrypt;


import org.senseframework.support.core.utils.DigestUtils;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.GeneralSecurityException;

/**
 * HMAC-SHA1签名算法
 *
 * @author sswang
 */
public class HmacSha1Algorithm extends SignatureAlgorithm {

    /**
     * 算法内部名
     */
    private static final String ALGORITHM_NAME = "HmacSHA1";

    @Override
    public String signature(byte[] key, byte[] content) throws GeneralSecurityException {
        String sign = null;
        // 初始化算法
        SecretKey sKey = new SecretKeySpec(key, ALGORITHM_NAME);
        Mac mac = Mac.getInstance(ALGORITHM_NAME);
        mac.init(sKey);
        // 执行签名
        byte[] buf = mac.doFinal(content);
        // 将结果用Base64编码
        sign = DigestUtils.encodeBase64(buf);
        return sign;
    }

}
